package jh.poj.builder;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class HTMLDocumentBuilder implements DocumentBuilder {
	private interface IHtmlElement{
		String create();
	}
	
	
	private class HtmlString implements IHtmlElement{
		private final String content;
		private final int tabs;
		public HtmlString(String title,int tabs) {
			this.content = title;
			this.tabs=tabs;
		}
		@Override
		public String create() {
			StringBuffer result=new StringBuffer();
			StringTokenizer tok=new StringTokenizer(content,"\n");
			while(tok.hasMoreTokens()){
				result.append(getTabs()+tok.nextToken()+"<br>\n");
			}
			return result.toString();
		}
		private String getTabs() {
			StringBuffer result = new StringBuffer();
			for (int i = 0; i < tabs; i++) 
				result.append("\t");
			return result.toString();
		}
		
	}
	
	
	private class HTMLTag implements IHtmlElement{
		final private String tag;
		final private String parameters;
		final private ArrayList<IHtmlElement> content;
		final private  int tabs;
		public HTMLTag(String tag,int tabs) {
			this.tag=tag;
			this.tabs=tabs;
			this.parameters=null;
			content=new ArrayList<IHtmlElement>();
		}
		public HTMLTag(String tag,String parameters,int tabs) {
			this.tag=tag;
			this.parameters=parameters;
			this.tabs=tabs;
			content=new ArrayList<IHtmlElement>();
		}
		
		public String create(){
			StringBuffer result=new StringBuffer();
			result.append(getTabs());
			if(parameters==null)
				result.append("<"+tag+">\n");
			else
				result.append("<"+tag+" "+parameters+">\n");
			
			for (IHtmlElement contentTag : content) {
				result.append(contentTag.create());
			}
			result.append(getTabs()+"</"+tag+">\n");
			return result.toString();	
		}
		
		public HTMLTag addString(HtmlString element){
			content.add(element);
			return this;
		}
		
		public HTMLTag addTag(HTMLTag element){
			content.add(element);
			return element;
		}
		private String getTabs() {
			StringBuffer result = new StringBuffer();
			for (int i = 0; i < tabs; i++)
				result.append("\t");
			return result.toString();
		}
	}

	HTMLTag html;
	HTMLTag body;
	HTMLTag title;
	HTMLTag currentChapter;
	String chapterParameter;
	String author;
	int currentChapterLevel;
	int currentTabs=0;
	final private ChaptersNumering mChaptersNumering;
	public HTMLDocumentBuilder(ChaptersNumering mChaptersNumering) {
		super();
		this.currentChapterLevel = 0;
		this.html = new HTMLTag("html", currentChapterLevel);
		this.body = new HTMLTag("body", currentChapterLevel);
		
		this.mChaptersNumering=mChaptersNumering;
		if(mChaptersNumering==ChaptersNumering.NORMAL)
			chapterParameter="type=\"1\"";
		else
			chapterParameter="type=\"none\"";
		
		currentChapter=new HTMLTag("OL", chapterParameter, currentChapterLevel);
		body.addTag(currentChapter);
		
	}

	private String getTabs() {
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < currentChapterLevel; i++) {
			result.append("\t");
		}
		return result.toString();
	}
	private String getTabs(int level) {
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < level; i++) {
			result.append("\t");
		}
		return result.toString();
	}

	@Override
	public DocumentBuilder addTitle(String title) {
		
		this.title= new HTMLTag("head", 1)
		.addTag(new HTMLTag("title", 2))
		.addString(new HtmlString(title,2));
		return this;
	}

	@Override
	public DocumentBuilder addAuthor(String author) {
		this.author=author;
		return this;
	}

	@Override
	public DocumentBuilder addChapter(String chapterName, int level) {
		if(level==currentChapterLevel){
			
			currentChapter.addTag(new HTMLTag("li", currentChapterLevel+1))
			.addString(new HtmlString(chapterName,currentChapterLevel+2));
			
		}
		if(level>currentChapterLevel){
		
			HTMLTag tag=new HTMLTag("OL", chapterParameter, level);
			currentChapter.addTag(tag)
			.addTag(new HTMLTag("li", level+1))
			.addString(new HtmlString(chapterName,level+2));
			currentChapter=tag;
		}

		
		
		currentChapterLevel=level;
		return this;
	}

	@Override
	public DocumentBuilder addParagraph(String paragraphText) {

		return this;
	}

	public String create() {
		html.addTag(title);
		html.addTag(body);
		
		
		return html.create();

	}

	void saveToFile(String path) {
		if (path == null)
			throw new IllegalArgumentException("path is null");
		if (path.isEmpty())
			throw new IllegalArgumentException("path is empty");

		FileWriter file;
		try {
			file = new FileWriter(path + ".html");
			file.write(create());
			file.close();
		} catch (IOException e) {
			System.out.println("problem with file:" + e.getMessage());
		}

	}

	@Override
	public DocumentBuilder addBulleted(String bullet) {
		// TODO Auto-generated method stub
		return this;
	}
}
